<?php
namespace Admin\Controller;

use Admin\Controller\CommonController;

/**
 * 模块
 * @author wangdong
 */
class FeedbackController extends CommonController
{
    public function index($draw = 1, $start = 0, $length = 10, $sort = 'id', $order = array(array('dir' => 'desc')))
    {
        //站点列表
        if (IS_POST) {
            $dbSite = M('Feedback');
            $total = $dbSite->count();
            $list = array();
            if ($total > 0) {
                $order = $sort . ' ' . $order[0]['dir'];
                $limit = $start . "," . $length;
                $list = $dbSite->table('__FEEDBACK__ as t')->field("t.*,FROM_UNIXTIME(t.addtime, \"%Y-%m-%d %H:%i:%s\") as addtime,c.name car")
                    ->join("LEFT JOIN __CAR__ c on t.carId = c.id")
                    ->limit($limit)
                    ->order($order)
                    ->select();
            }
            $data = array(
                "draw" => $draw,
                "recordsTotal" => $total,
                "recordsFiltered" => $total,
                "data" => $list
            );
            $this->ajaxReturn($data);
        } else {
            $this->display();
        }

    }


    public function agent()
    {
        $feedbackdb = M('Feedback');
        $_validate = array(
            array('id', 'require', '数据错误！'),
            array('result', 'require', '处理结果必须填写')
        );
        if (!$feedbackdb->validate($_validate)->create()) {
            // 如果创建失败 表示验证没有通过 输出错误提示信息
            $this->error($feedbackdb->getError());
        } else {
            $data = I('post.');
            $id = $data['id'];
            unset($data['id']);
            if($data['result']==1&&$data['type']==2){

            }
            $state = $feedbackdb->where('id=' . $id)->save($data);
            $state ? $this->success('修改成功') : $this->error('修改失败');
        }
    }

    public function shareInfo(){
        if(IS_POST) {
            $feedbackdb = M('Feedback');
            $_validate = array(
                array('id', 'require', '数据错误！'),
                array('content', 'require', '分享信息必须填写！'),
            );
            if (!$feedbackdb->validate($_validate)->create()) {
                // 如果创建失败 表示验证没有通过 输出错误提示信息
                $this->error($feedbackdb->getError());
            } else {
                $data = I('post.');
                $id = $data['id'];
                unset($data['id']);
                $state = $feedbackdb->where('id=' . $id)->save(array('result' => 1,'content'=>$data['content']));
                if ($state) {
                    //发送通知
                    $feedback = $feedbackdb->where('id=' . $id)->find();
                    require_once APP_PATH . "Common/Lib/push/sdk.php";
                    $message = array(
                        // 消息的标题.
                        'title' => '一条路况信息，请注意查看',
                        // 消息内容
                        'description' => $feedback['content'],
                        "custom_content" => array("type" => "3", 'id' => $id)
                    );
                    // 设置消息类型为 通知类型.
                    $opts = array(
                        'msg_type' => 1,
                        'msg_expires ' => 3600*24
                    );

                    // 创建SDK对象.
                    $sdk = new \PushSDK();
                    // 向所有设备发送一条消息
                    $rs = $sdk->pushMsgToAll($message, $opts);
                    // 判断返回值,当发送失败时, $rs的结果为false, 可以通过getError来获得错误信息.
                    if ($rs === false) {
//                print_r($sdk->getLastErrorCode());
//                print_r($sdk->getLastErrorMsg());
                        $this->error('发送任务失败!错误:' . $sdk->getLastErrorCode() . '  描述:' . $sdk->getLastErrorMsg());
                    }
                }
                $state ? $this->success('修改成功') : $this->error('修改失败');
            }
        }else{
            $id = I('get.id', 0);
            if ($id > 0) {
                $feedbackdb = M('Feedback');
                $feedback = $feedbackdb->where('id=' . $id)->find();
                $this->assign('data', $feedback);
            }
            $this->display();
        }
    }

    public function deleteItem(){
        $feedbackdb = M('Feedback');
        $_validate = array(
            array('id', 'require', '数据错误！'),
        );
        if (!$feedbackdb->validate($_validate)->create()) {
            // 如果创建失败 表示验证没有通过 输出错误提示信息
            $this->error($feedbackdb->getError());
        } else {
            $data = I('post.');
            $id = $data['id'];
            $feedbackdb->where('id=' . $id)->delete();
            $this->success('删除成功');
        }
    }

}